//
//  BN_MedalDisPViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-23.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_MedalDisPViewController.h"

#define NAVGATIONVIEW_HEIGHT 150
static int ITEM_COUNT = 0;

@interface BN_MedalDisPViewController () {
    NSArray *itemNameArr_;
    NSArray *itemImg_unmedalArr_;
    NSArray *itemImg_medalArr_;

    BN_MyInfoHelper *myInfoHelper_;
}

@end

@implementation BN_MedalDisPViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // NSObject
    itemNameArr_ = @[@"全勤",@"懂礼貌",@"讲卫生",@"不挑食",@"爱思考",@"守纪律"];
    itemImg_unmedalArr_ = @[@"quanqingdianji",@"donglimaodianji",@"jiangweishengdianji",@"butiaoshidianji",@"aisikaodianji",@"soujilvdianji"];
    itemImg_medalArr_ = @[@"quanqing",@"donglimao",@"jiangweisheng",@"butiaoshi",@"aisikao",@"shoujilv"];
    
    self.medalDataArray = @[@{@"medaNumb": @"0"},@{@"medaNumb": @"0"},@{@"medaNumb": @"0"},@{@"medaNumb": @"0"},@{@"medaNumb": @"0"},@{@"medaNumb": @"0"},@{@"medaNumb": @"0"},@{@"medaNumb": @"0"},@{@"medaNumb": @"0"}];
    
    // 网络帮助类
    myInfoHelper_ = [[BN_MyInfoHelper alloc]init];
    
    // 风火轮
    [self viewLoadHintView];
    [self medalNavgationView];
    [self performSelectorInBackground:@selector(requestNetData) withObject:nil];
}

- (void)medalNavgationView {
    
    for (UIView *view in self.view.subviews) {
        [view removeFromSuperview];
    }
    UIButton *navgationView = [[UIButton alloc]init];
    switch (MAIN_VERSION) {
        case 6:
            navgationView.frame = CGRectMake(0, -10, SCREEN_WIDTH, NAVGATIONVIEW_HEIGHT);
        default:
            navgationView.frame = CGRectMake(0, 0, SCREEN_WIDTH, NAVGATIONVIEW_HEIGHT);
            break;
    }
    UIColor *color = APP_OBJECT_GREEN_COLOR;
    navgationView.backgroundColor = color;
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, (64-44)/2+10, 44, 44)];
    [backBtn setImage:[UIImage imageNamed:@"houtui"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backRootVc) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *totalCountImgView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-150)/2+26, (NAVGATIONVIEW_HEIGHT-130)/2+10, 150, 130)];
    totalCountImgView.image = [UIImage imageNamed:@"medalTotalCount"];
    [navgationView addSubview:totalCountImgView];
    [navgationView addSubview:backBtn];
    
    // 勋章总数显示文本
    UILabel *medalTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(23, 30, 100, 13)];
    medalTextLabel.textColor = [UIColor whiteColor];
    medalTextLabel.text =  @"勋章总数";
    medalTextLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:13];
    medalTextLabel.backgroundColor = [UIColor clearColor];
    [totalCountImgView addSubview:medalTextLabel];
    
    // 勋章总数显示数量
    UILabel *medalCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(21, 45, 53, 30)];
    medalCountLabel.textColor = [UIColor whiteColor];
    medalCountLabel.text =  self.medalCountStr;
    medalCountLabel.textAlignment = NSTextAlignmentCenter;
    medalCountLabel.backgroundColor = [UIColor clearColor];
    medalCountLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:30];
    [totalCountImgView addSubview:medalCountLabel];
    [self.view addSubview:navgationView];
}

- (void)medalItemTogroup {
    // 清除scrollView视图
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            [view removeFromSuperview];
        }
    }
    UIScrollView *collectScrollView = [[UIScrollView alloc]init];
    
    switch (MAIN_VERSION) {
        
        case 6:
            collectScrollView.frame  = CGRectMake(0, NAVGATIONVIEW_HEIGHT-10, SCREEN_WIDTH, SCREEN_HEIGHT-NAVGATIONVIEW_HEIGHT+10);
            break;
        default:
            collectScrollView.frame  = CGRectMake(0, NAVGATIONVIEW_HEIGHT, SCREEN_WIDTH, FULLSCREEN_HEIGHT-NAVGATIONVIEW_HEIGHT);
            break;
    }
    [self.view addSubview:collectScrollView];
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            // code...
            // UIView
            UIView *collectView = [[UIView alloc]initWithFrame:CGRectMake(159*j+j*2, 140*i, 160, 139)];
            collectView.backgroundColor = [UIColor whiteColor];
            [collectScrollView addSubview:collectView];
            
            // UIImageView
            UIImageView *itemImageView = [[UIImageView alloc]initWithFrame:CGRectMake((160-77)/2, (139-77)/2-10, 77, 77)];
            [collectView addSubview:itemImageView];
            
            // 勋章数
            NSString *medaNumbStr = [NSString stringWithFormat:@"%@",self.medalDataArray[ITEM_COUNT][@"medaNumb"]];

            if ([medaNumbStr isEqualToString:@"0"]) {
                itemImageView.image = [UIImage imageNamed:itemImg_unmedalArr_[ITEM_COUNT]];
            }else {
                itemImageView.image = [UIImage imageNamed:itemImg_medalArr_[ITEM_COUNT]];
                
                UILabel *itemCountLabel = [[UILabel alloc]initWithFrame:CGRectMake((72-50)/2, (66-20)/2, 50, 20)];
                itemCountLabel.textAlignment = NSTextAlignmentCenter;
                itemCountLabel.backgroundColor = [UIColor clearColor];
                itemCountLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:20];
                itemCountLabel.textColor = [UIColor whiteColor];
                itemCountLabel.text = medaNumbStr;
                [itemImageView addSubview:itemCountLabel];
            }
 
            // UILabel
            UILabel *itemLabel = [[UILabel alloc]initWithFrame:CGRectMake((160-77)/2, (139-77)/2+77, 77, 14)];
            itemLabel.text = itemNameArr_[ITEM_COUNT];
            itemLabel.font = [UIFont systemFontOfSize:14];
            itemLabel.backgroundColor = [UIColor clearColor];
            itemLabel.textAlignment = NSTextAlignmentCenter;
            [collectView addSubview:itemLabel];
            ITEM_COUNT++;
        }
    }
    [collectScrollView setContentSize:CGSizeMake(0, 139*3)];
}

#pragma mark 网络请求
- (void)requestNetData {
    __weak typeof(self) weakSelf = self;
    [myInfoHelper_ queryMedalList:^(id result, NSString *errMsg) {
        if (errMsg) {
            [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
            [self cancelHintView];
        }else{
            [weakSelf modifyDict:result];
            [self cancelHintView];
        }
    }];
}

- (void)modifyDict:(id)result {
    self.netDict = (NSDictionary *)result;
    self.medalCountStr = self.netDict[@"medaCoun"];
    self.chilIdStr = self.netDict[@"chilId"];
    self.medalDataArray = self.netDict[@"medaDeta"];
    
    ITEM_COUNT = 0;
    [self performSelectorInBackground:@selector(medalNavgationView) withObject:nil];
    [self performSelectorInBackground:@selector(medalItemTogroup) withObject:nil];
}

#pragma mark
#pragma mark Action
- (void)backRootVc {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 资源

- (void)releaseResource {
    ITEM_COUNT = 0;
    [myInfoHelper_ cancleQueryEvalList];
    myInfoHelper_ = nil;
}

- (void)dealloc
{
    [self releaseResource];
    NSLog(@"勋章展示界面释放...");
}

@end
